package com.example.enterprise

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.enterprise.adapter.MerchantAdapter
import com.example.enterprise.databinding.ActivityMerchantBinding
import com.example.enterprise.entity.MerchantAddParam
import com.example.enterprise.entity.ProcessingStep
import com.example.enterprise.vm.MerchantViewModel
import com.example.image.glide.GlideImgLoader
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import kotlin.random.Random

@AndroidEntryPoint
@Route (path = "/enterprise/MerchantActivity")
class MerchantActivity : BaseMvvmActivity<ActivityMerchantBinding>() {
    override fun getLayoutId(): Int=R.layout.activity_merchant
    val merchantAdapter=MerchantAdapter()
    val merchantViewModel:MerchantViewModel by viewModels ()
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->{
                    mDataBinding.rb1.isChecked=true
                    mDataBinding.rb2.isChecked=false
                }
                R.id.rb2->{
                    mDataBinding.rb1.isChecked=false
                    mDataBinding.rb2.isChecked=true
                }
            }
            if (mDataBinding.rb1.isChecked){
                mDataBinding.ll1.visibility=View.VISIBLE
                mDataBinding.ll2.visibility=View.GONE
            }else{
                mDataBinding.ll1.visibility=View.GONE
                mDataBinding.ll2.visibility=View.VISIBLE
                merchantViewModel.getMerchant()
            }
        }
        //图片
        mDataBinding.add.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        //跳转到入驻审批
        mDataBinding.gotoApproval.setOnClickListener {
            ARouter.getInstance().build("/enterprise/CheckApproveActivity").navigation()
        }
        //添加
        val random = Random.nextInt(899999)+100000
        mDataBinding.submit.setOnClickListener {
           var merchantAddParam=MerchantAddParam(
               applicationArea=mDataBinding.addEnterpriseApplicationArea.text.toString().toInt(),
             applicationNumber="RZ"+random,
             contactMethod=1395566788,
             contactName= "哈基米",
             documents= emptyList(),
             enterpriseName= mDataBinding.addEnterpriseName.text.toString(),
             enterpriseProfileId=1,
             entryPersonnel= mDataBinding.addEnterprisePeopleCount.text.toString().toInt(),
             entryTime= SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()),
             id = 0,
             processingSteps= emptyList(),
             status="新建"
            )
            merchantViewModel.getAddMerchant(merchantAddParam)
            merchantViewModel.mSuccessLiveData.observe(this){
                ToastUtils.showLong("添加成功")
            }
            merchantViewModel.mErrorLiveData.observe(this){
                ToastUtils.showLong(it)
            }
        }
        //数据
        mDataBinding.rv.adapter=merchantAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        merchantViewModel.mSuccessLiveData.observe(this){
            merchantAdapter.data.clear()
            merchantAdapter.data.addAll(it.data.sortedByDescending { it.id })
            merchantAdapter.notifyDataSetChanged()
        }
        merchantViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        merchantAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/enterprise/MerchantXiangActivity")
                .withInt("merchanId",merchantAdapter.data[position].id)
                .navigation()
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== RESULT_OK){
            val bean=data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.add)
        }
    }
}